"""
{
    "question": "总共有多少参赛选手参加23届冬奥会瑞典代表团？", 
    "answer": "116人", 
    "Q": "参加23届冬奥会的瑞典代表团一共有多少选手？", 
    "ansable": "Y",
    "candidates": [
        {
            "answer": "116人",
            "question": "总共有多少参赛选手参加23届冬奥会瑞典代表团？",
            "judgeScore": "@",
        },
        {
            "answer": "116人",
            "question": "共有多少参赛选手参加23届冬奥会瑞典代表团？",
            "judgeScore": "@",
        },
        {
            "answer": "116人",
            "question": "共有多少参赛选手参加23届冬奥会法国代表团？",
            "judgeScore": "",
        }
    ]
}

<O>总共有多少参赛选手参加23届冬奥会瑞典代表团？</O>
<Q>参加23届冬奥会的瑞典代表团一共有多少选手？</Q>
<1>总共有多少参赛选手参加23届冬奥会瑞典代表团？</1>@
<2>共有多少参赛选手参加23届冬奥会瑞典代表团？</2>@
<3>共有多少参赛选手参加23届冬奥会法国代表团？</3>
"""
import json

target = "data/洪宇标注结果.txt"
output = "data/洪宇标注结果.jsonl"
f = open(target, 'r', encoding='utf-8')
content = f.readlines()
f.close()

js_list = []
temp_js = {}
temp_js['question'] = ""
temp_js['Q'] = ""
temp_js['candidates'] = []

for line in content:
    line = line.strip()
    if line[:3] == "<O>":
        if line[-1] in ["!", "！"]:
            temp_js['answerable'] = "N"
            temp_js['question'] = line[3:-5]
        else:
            temp_js['answerable'] = "Y"
            temp_js['question'] = line[3:-4]
    elif line[:3] == "<Q>":
        if line[-1] in ["!", "！"]:
            temp_js['Q'] = line[3:-5]
        else:
            temp_js['Q'] = line[3:-4]
    elif line[:3] == "<1>":
        candidate = {}
        if line[-1] == "@":
            candidate['judgeScore'] = "@"
            candidate['question'] = line[3:-5]
        else:
            candidate['judgeScore'] = ""
            candidate['question'] = line[3:-4]

        temp_js['candidates'].append(candidate)
    elif line[:3] == "<2>":
        candidate = {}
        if line[-1] == "@":
            candidate['judgeScore'] = "@"
            candidate['question'] = temp_js['question'] = line[3:-5]
        else:
            candidate['judgeScore'] = ""
            candidate['question'] = temp_js['question'] = line[3:-4]

        temp_js['candidates'].append(candidate)
    elif line[:3] == "<3>":
        candidate = {}
        if line[-1] == "@":
            candidate['judgeScore'] = "@"
            candidate['question'] = temp_js['question'] = line[3:-5]
        else:
            candidate['judgeScore'] = ""
            candidate['question'] = temp_js['question'] = line[3:-4]
        temp_js['candidates'].append(candidate)

        js_list.append(temp_js)
        temp_js = {}
        temp_js['question'] = ""
        temp_js['Q'] = ""
        temp_js['candidates'] = []

f = open(output, 'w', encoding='utf-8')
for line in js_list:
    line = json.dumps(line, ensure_ascii=False)
    f.write(line+'\n')

f.close()